import numpy as np
import matplotlib.pyplot as plt

# 生成示例数据
x = np.random.rand(100) * 10
y = np.random.rand(100) * 10
z = np.random.rand(100) * 100  # 数据值

# 定义网格的分辨率
resolution = 0.1

# 计算网格单元的数量
x_bins = int((np.max(x) - np.min(x)) / resolution)
y_bins = int((np.max(y) - np.min(y)) / resolution)

# 数据聚合
heatmap, _, _ = np.histogram2d(x, y, bins=[x_bins, y_bins], weights=z)

# 数据插值
x_values = np.linspace(np.min(x), np.max(x), x_bins)
y_values = np.linspace(np.min(y), np.max(y), y_bins)
x_grid, y_grid = np.meshgrid(x_values, y_values)

# 绘制热力图
plt.imshow(heatmap.T, origin='lower', extent=[np.min(x), np.max(x), np.min(y), np.max(y)], cmap='hot')
plt.colorbar()
plt.scatter(x, y, c=z, cmap='hot', edgecolors='white', linewidths=0.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Heatmap Example')
plt.show()
